// import { pinia } from '@/store';
import { eLocalStorageConfig } from '@/interface/elocal_storage';
import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';

// import { useUserInfo } from '@/store/user';
import NProgress from 'nprogress';
import { main_router } from './main';

//全局进度条的配置
NProgress.configure({
  easing: 'ease', // 动画方式
  speed: 1000, // 递增进度条的速度
  showSpinner: true, // 是否显示加载ico
  trickleSpeed: 200, // 自动递增间隔
  minimum: 0.3, // 更改启动时使用的最小百分比
  parent: 'body', //指定进度条的父容器
});

const routes: Array<RouteRecordRaw> = [
  main_router,
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/index.vue'),
  },
  {
    path: '/welcome',
    name: 'welcome',
    component: () => import('@/layout/TopAndBottom.vue'),
    children: [
      {
        path: '/welcome',
        name: 'welcome1',
        component: () => import('@/views/welcome/index.vue'),
      },
    ],
  },
  {
    // 404
    path: '/404',
    name: '404',
    component: () => import('@/views/404/index.vue'),
  },
  {
    // 404
    path: '/:pathMatch(.*)*',
    component: () => import('@/views/welcome/index.vue'),
  },
];

export const routers = createRouter({
  history: createWebHashHistory(),
  routes,
});
// setTimeout(() => {
//     console.log('动态添加路由');
//     routers.addRoute(main_router);
//     routers.push("/main/home")
// }, 3000);

routers.beforeEach((to: any, _: any, next) => {
  NProgress.start();
  // 判断跳转的页面是否有用户登录信息
  const token = localStorage.getItem(eLocalStorageConfig.TOKEN);
  if (to.name !== 'login' && to.name !== '404') {
    if (token) {
      next(true);
    } else {
      if (to.query.token) {
        next(true);
      } else {
        //TODO 暂时无条件放过
        next(true);
        // next({
        //     name: 'login'
        // });
      }
    }
  } else {
    if (token) {
      next({
        path: '/',
      });
    } else {
      next(true);
    }
  }
});

routers.afterEach(() => {
  NProgress.done();
});

export const getMainLayout = () => {
  return import('@/layout/MainLayout.vue');
};

export const getContentLayout = () => {
  return import('@/layout/ContentLayout.vue');
};
